/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SelectDateDialog.java
 *
 * Created on 13-Nov-2010, 15:57:19
 */

package TimeRecording.forms;

import TimeRecording.business.DaisyChainSystem;
import TimeRecording.business.DaisyChainUtilities;
import java.awt.CardLayout;
import java.util.Date;
import java.util.GregorianCalendar;
import org.jdesktop.application.Action;

/**
 *
 * @author david
 */
public class SelectDateDialog extends javax.swing.JDialog {

    public enum DateType {
        DAY,
        WEEK,
        MONTH,
        YEAR
    }

    // <editor-fold defaultstate="collapsed" desc="selectedDate + Getter & Setter">
    private GregorianCalendar selectedCalendar;

    public GregorianCalendar getSelectedCalendar() {
        return selectedCalendar;
    }

    public void setSelectedCalendar(GregorianCalendar selectedCalendar) {
        GregorianCalendar oldSelectedCalendar = this.selectedCalendar;
        this.selectedCalendar = selectedCalendar;
        firePropertyChange("selectedCalendar", oldSelectedCalendar, selectedCalendar);
    } // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="dateType + Getter & Setter">
    private DateType dateType;

    public DateType getDateType() {
        return dateType;
    }

    public void setDateType(DateType dateType) {
        DateType oldDateType = this.dateType;
        this.dateType = dateType;
        firePropertyChange("dateType", oldDateType, dateType);
    } // </editor-fold>

    /** Creates new form SelectDateDialog */
    public SelectDateDialog(java.awt.Window parent, DateType dateType) {

        super(parent, ModalityType.APPLICATION_MODAL);
        initComponents();
        this.dateType = dateType;
        
        CardLayout layout = (CardLayout)editorPanel.getLayout();
        GregorianCalendar today = new GregorianCalendar();
        today.setTime(DaisyChainSystem.cleanDate(new Date()));

        switch (dateType) {
            case DAY:
                layout.show(editorPanel, "Day Panel");
                dayCombo.setDate(today.getTime());
                break;
            case WEEK:
                layout.show(editorPanel, "Week Panel");
                weekCombo.setDate(today.getTime());
                break;
            case MONTH:
                layout.show(editorPanel, "Month Panel");
                monthYearSpinner.setValue(today.get(GregorianCalendar.YEAR));
                monthCombo.setSelectedIndex(today.get(GregorianCalendar.MONTH));
                break;
            case YEAR:
                layout.show(editorPanel, "Year Panel");
                yearSpinner.setValue(today.get(GregorianCalendar.YEAR));
                break;
        }

        setSelectedCalendar(null);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        editorPanel = new javax.swing.JPanel();
        dayPanel = new javax.swing.JPanel();
        dayLabel = new javax.swing.JLabel();
        dayCombo = new com.toedter.calendar.JDateChooser();
        weekPanel = new javax.swing.JPanel();
        weekLabel = new javax.swing.JLabel();
        weekCombo = new com.toedter.calendar.JDateChooser();
        monthPanel = new javax.swing.JPanel();
        monthYearLabel = new javax.swing.JLabel();
        monthYearSpinner = new javax.swing.JSpinner();
        monthLabel = new javax.swing.JLabel();
        monthCombo = new javax.swing.JComboBox();
        yearPanel = new javax.swing.JPanel();
        yearLabel = new javax.swing.JLabel();
        yearSpinner = new javax.swing.JSpinner();
        buttonPanel = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(TimeRecording.TimeRecordingApp.class).getContext().getResourceMap(SelectDateDialog.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        getContentPane().setLayout(new java.awt.GridBagLayout());

        editorPanel.setName("editorPanel"); // NOI18N
        editorPanel.setLayout(new java.awt.CardLayout());

        dayPanel.setName("dayPanel"); // NOI18N

        dayLabel.setText(resourceMap.getString("dayLabel.text")); // NOI18N
        dayLabel.setName("dayLabel"); // NOI18N
        dayPanel.add(dayLabel);

        dayCombo.setMinimumSize(new java.awt.Dimension(150, 20));
        dayCombo.setName("dayCombo"); // NOI18N
        dayCombo.setPreferredSize(new java.awt.Dimension(120, 20));
        dayPanel.add(dayCombo);

        editorPanel.add(dayPanel, "Day Panel");

        weekPanel.setName("weekPanel"); // NOI18N

        weekLabel.setText(resourceMap.getString("weekLabel.text")); // NOI18N
        weekLabel.setName("weekLabel"); // NOI18N
        weekPanel.add(weekLabel);

        weekCombo.setMinimumSize(new java.awt.Dimension(150, 20));
        weekCombo.setName("weekCombo"); // NOI18N
        weekCombo.setPreferredSize(new java.awt.Dimension(120, 20));
        weekPanel.add(weekCombo);

        editorPanel.add(weekPanel, "Week Panel");

        monthPanel.setName("monthPanel"); // NOI18N

        monthYearLabel.setText(resourceMap.getString("monthYearLabel.text")); // NOI18N
        monthYearLabel.setName("monthYearLabel"); // NOI18N
        monthPanel.add(monthYearLabel);

        monthYearSpinner.setEditor(new javax.swing.JSpinner.NumberEditor(monthYearSpinner, "0"));
        monthYearSpinner.setName("monthYearSpinner"); // NOI18N
        monthYearSpinner.setPreferredSize(new java.awt.Dimension(80, 20));
        monthPanel.add(monthYearSpinner);

        monthLabel.setText(resourceMap.getString("monthLabel.text")); // NOI18N
        monthLabel.setName("monthLabel"); // NOI18N
        monthPanel.add(monthLabel);

        monthCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }));
        monthCombo.setName("monthCombo"); // NOI18N
        monthCombo.setPreferredSize(new java.awt.Dimension(100, 20));
        monthPanel.add(monthCombo);

        editorPanel.add(monthPanel, "Month Panel");

        yearPanel.setName("yearPanel"); // NOI18N

        yearLabel.setText(resourceMap.getString("yearLabel.text")); // NOI18N
        yearLabel.setName("yearLabel"); // NOI18N
        yearPanel.add(yearLabel);

        yearSpinner.setEditor(new javax.swing.JSpinner.NumberEditor(yearSpinner, "0"));
        yearSpinner.setName("yearSpinner"); // NOI18N
        yearSpinner.setPreferredSize(new java.awt.Dimension(80, 20));
        yearPanel.add(yearSpinner);

        editorPanel.add(yearPanel, "Year Panel");

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        getContentPane().add(editorPanel, gridBagConstraints);

        buttonPanel.setName("buttonPanel"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(TimeRecording.TimeRecordingApp.class).getContext().getActionMap(SelectDateDialog.class, this);
        okButton.setAction(actionMap.get("ok")); // NOI18N
        okButton.setText(resourceMap.getString("okButton.text")); // NOI18N
        okButton.setName("okButton"); // NOI18N
        buttonPanel.add(okButton);

        cancelButton.setAction(actionMap.get("cancel")); // NOI18N
        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setDefaultCapable(false);
        cancelButton.setName("cancelButton"); // NOI18N
        buttonPanel.add(cancelButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        getContentPane().add(buttonPanel, gridBagConstraints);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-321)/2, (screenSize.height-118)/2, 321, 118);
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void ok() {
        
        GregorianCalendar cal = new GregorianCalendar();

        switch (dateType) {
            case DAY:
                cal.setTime(dayCombo.getDate());
                setSelectedCalendar(cal);
                break;
            case WEEK:
                cal.setTime(weekCombo.getDate());
                setSelectedCalendar(DaisyChainUtilities.firstDayOfWeek(cal));
                break;
            case MONTH:
                setSelectedCalendar(new GregorianCalendar(
                        (Integer)monthYearSpinner.getValue(),
                        monthCombo.getSelectedIndex(), 1));
                break;
            case YEAR:
                setSelectedCalendar(new GregorianCalendar(
                        (Integer)yearSpinner.getValue(), 0, 1));
                break;
        }

        setVisible(false);
    }

    @Action
    public void cancel() {
        setVisible(false);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton cancelButton;
    private com.toedter.calendar.JDateChooser dayCombo;
    private javax.swing.JLabel dayLabel;
    private javax.swing.JPanel dayPanel;
    private javax.swing.JPanel editorPanel;
    private javax.swing.JComboBox monthCombo;
    private javax.swing.JLabel monthLabel;
    private javax.swing.JPanel monthPanel;
    private javax.swing.JLabel monthYearLabel;
    private javax.swing.JSpinner monthYearSpinner;
    private javax.swing.JButton okButton;
    private com.toedter.calendar.JDateChooser weekCombo;
    private javax.swing.JLabel weekLabel;
    private javax.swing.JPanel weekPanel;
    private javax.swing.JLabel yearLabel;
    private javax.swing.JPanel yearPanel;
    private javax.swing.JSpinner yearSpinner;
    // End of variables declaration//GEN-END:variables

}
